home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 713 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.5 KB

  1. From: kanze@gabi-soft.fr (J. Kanze)
  2. Message-ID: <KANZE.96Mar14131235@gabi.gabi-soft.fr>
  3. X-Original-Date: 14 Mar 1996 12:12:35 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 14 Mar 96 15:14:00 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: String value of enum
  9. Organization: GABI Software, Sarl.
  10. References: <4i5sf3$89c@hermes.is.co.za> <Do81tp.H9u@rsvl.unisys.com>
  11. In-Reply-To: mtm4@rsvl.unisys.com's message of 13 Mar 96 23:40:59 GMT
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMUg3zuEDnX0m9pzZAQGpwAF9G7YayO/+a9nUhFwaqjk06pcMoK1rtqES
  14.     L27nJ4LV6H7uRmF7qR8DmjMogqZmNU1q
  15.     =ypVB
  16.  
  17. In article <Do81tp.H9u@rsvl.unisys.com> mtm4@rsvl.unisys.com (Michael
  18. McCormick) writes:
  19.  
  20. |> "W. Dicks" <wd@isis.co.za> shared the following on 13 Mar 96 07:31:13 GMT:
  21.  
  22. |> >The system that I'm working often needs to know the string 
  23. |> >value of an enum. e.g. enum week{MON=1, ..., SUN} it; it = 
  24. |> >TUE;
  25. |> >Then when this enum is passed as a parameter the function 
  26. |> >should return the string based on the enum. For instance, 
  27. |> >weekImage(it) will return "TUE". Is it not possible that such 
  28. |> >a functionality can be built into enums?
  29.  
  30. |> What you are essentially asking for is language support for converting
  31. |> a label into a string respresentation of its name.  That is a very
  32. |> unusual feature to find in any language, since it would require the
  33. |> compiler to place the symbolic dictionary in the executable file.  I
  34. |> don't think there's a snowball's chance of this getting into C++.
  35.  
  36. Well, it is supported in other languages.  But I agree that it will not
  37. be in C++, and should not be there, for an entirely different reason.
  38. What happens to such strings when you change locale?  (In practice, they
  39. are only good for debugging because of this.  And any decent debugger
  40. will be able to display the information without the table in the
  41. compiler.)
  42.  
  43. |> Since enum is by definition an integral type, why not assign values to your
  44. |> enumerators that can be used as indexes into a string?:
  45.  
  46. |>  enum week {MON=0,TUE=4,WED=8,THU=12,FRI=16,SAT=20,SUN=24};
  47. |>  const char * days = "MON\0TUE\0WED\0THU\0FRI\0SAT\0SUN";
  48.  
  49. |>  char const * weekImage(week day) = days[day];
  50.  
  51. Now this is going to be really fun to maintain.  What happens when the
  52. program is prepared for internationalization, and the translater
  53. modifies the strings?
  54.  
  55. Using:
  56.  
  57.     char const *const    weekdayNames[] = { "Monday" , ... } ;
  58.  
  59. and indexing with the original enum is considerably better.  In fact, in
  60. this one particular case, it is probably acceptable.  I cannot imagine
  61. any conceivable change in specification which would cause the number of
  62. days in a week to change.  This is a special case, however.
  63.  
  64. For the general case, I wrote a template class which manages a map, and
  65. a small parser (just intelligent to extract the names from an enum, and
  66. ignore everything else) to generate the initialization data for the map.
  67. -- 
  68. James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
  69. GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
  70. Conseils, itudes et rialisations en logiciel orienti objet --
  71.               -- A la recherche d'une activiti dans une region francophone
  72. ---
  73. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  74. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  75. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  76. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  77. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  78.